package com.freesky.biz.activity.service.impl;

import com.freesky.biz.activity.domain.TSettlement;
import com.freesky.biz.activity.domain.TSettlementDetail;
import com.freesky.biz.activity.mapper.SettlementMapper;
import com.freesky.biz.activity.service.ISettlementService;
import com.freesky.common.constant.UserConstants;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SettlementServiceImpl implements ISettlementService {

    @Autowired
    private SettlementMapper settlementMapper;

    @Override
    public TSettlement get(String id) {
        return settlementMapper.get(id);
    }

    @Override
    public List<TSettlement> findList(TSettlement entity) {
        return settlementMapper.findList(entity);
    }

    @Override
    public boolean checkSettleNameUnique(TSettlement entity) {
        String settleId = entity.getSettleId();
        TSettlement info = settlementMapper.selectSettlementByName(entity.getSettleName());
        if (StringUtils.isNotNull(info) && !info.getSettleId().equalsIgnoreCase(settleId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<TSettlementDetail> findSettlementDetailList(TSettlementDetail entity) {
        return settlementMapper.findSettlementDetailList(entity);
    }

    @Override
    public int updateSettlementById(String fileUrl, String updateBy, String settleId) {
        int rows = settlementMapper.updateSettlementById(fileUrl, updateBy, settleId);
        updateCouponExpenseAmount(settleId);
        updateAlipayExpenseAmount(settleId);
        return rows;
    }

    @Override
    public int batchAddSettlementDetail(List<TSettlementDetail> settlementDetailList) {
        return settlementMapper.batchAddSettlementDetail(settlementDetailList);
    }

    @Override
    public int insert(TSettlement entity) {
        int rows = settlementMapper.insert(entity);
        updateCouponExpenseAmount(entity.getSettleId());
        updateAlipayExpenseAmount(entity.getSettleId());
        return rows;
    }

    @Override
    public int update(TSettlement entity) {
        int rows = settlementMapper.update(entity);
        updateCouponExpenseAmount(entity.getSettleId());
        updateAlipayExpenseAmount(entity.getSettleId());
        return rows;
    }

    @Override
    public int updateCouponExpenseAmount(String settleId) {
        return settlementMapper.updateCouponExpenseAmount(settleId);
    }

    @Override
    public int updateAlipayExpenseAmount(String settleId) {
        return settlementMapper.updateAlipayExpenseAmount(settleId);
    }

    @Override
    public int deleteByIds(String[] ids) {
        int rows = settlementMapper.deleteByIds(ids);
        updateCouponExpenseAmount(StringUtils.join(ids, ","));
        updateAlipayExpenseAmount(StringUtils.join(ids, ","));
        return rows;
    }
}
